<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="队伍名称">
              <a-input v-model="queryParam.name" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="24">
            <a-form-item label="负责人">
              <a-input v-model="queryParam.director" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="(!advanced && 6) || 24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
            >
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
             
            </span>
          </a-col>

          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="handleCreate">申请队伍</a-button>
          </a-col>

        </a-row>
      </a-form>
    </div>

     <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-has="'Volunteer:add'">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('志愿者队伍')" v-has="'Volunteer:export'">导出</a-button>
      <a-button type="primary" icon="import" @click="visibleImport=true" v-has="'Volunteer:import'">导入</a-button>
      <XImport v-model="visibleImport" moduleName="volunteerTeam" @ok="searchQuery" @cancel="visibleImport=false"></XImport>

      <a-dropdown v-if="selectedRowKeys.length > 0" v-has="'Volunteer:batchdelete'">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel" v-has="'Volunteer:batchdelete'"><a-icon type="delete"/>批量删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <s-table ref="table" size="default" :scroll="{x:true}" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" :columns="columns" :data="getData" @onSelect="onChange">
      <span slot="action" slot-scope="text, record">
        <a @click="handleReview(record)">查看</a>
        <a @click="handleEdit(record)" v-has="record.isDirector?'Force':'Volunteer:edit'" style="margin-left:10px">编辑</a>
        <a-popconfirm v-has="record.isDirector?'Force':'Volunteer:delete'" style="margin-left:10px" title="确定删除?" @confirm="() => handleDelete(record.id)">
          <a>删除</a>
        </a-popconfirm>
        <a style="margin-left:10px" @click="handleJoin(record)" v-has="'Volunteer:join'" :disabled="moment(record.dismissTime).isBefore(new Date())">申请加入</a>
      </span>
    </s-table>


    <!-- 社团弹窗 -->
    <VolunteerTeamModal ref="VolunteerTeamModal" @ok="searchQuery"></VolunteerTeamModal>

    <!-- 队伍成员弹窗 -->
    <VolunteerTeamMemberModal ref="VolunteerTeamMemberModal" ></VolunteerTeamMemberModal>

  </a-card>
</template>

<script>
import STable from '@/components/table/'
import XSelectUserByDep from '@/components/extends/XSelectUserByDep'
import XImport from '@/components/extends/XImport.vue'
import AInput from 'ant-design-vue/es/input/Input'
import moment from 'moment'
import VolunteerTeam from './modules/VolunteerTeam'
import Volunteer from './modules/Volunteer'
import VolunteerTeamModal from './modules/VolunteerTeamModal'
import VolunteerTeamMemberModal from './modules/VolunteerTeamMemberModal'

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction,postAction, deleteAction } from '@/api/manage'
import { getVolunteerTeams ,setVolunteerTeam,setVolunteerTeamMember} from '@/api/api'
import {merge} from 'lodash'
export default {
  name: 'TableList',
  components: {
    AInput,
    STable,
    XImport,
    VolunteerTeam,
    Volunteer,
    VolunteerTeamModal,
    VolunteerTeamMemberModal,
    XSelectUserByDep
  },
  mixins:[JeecgListMixin],
  data() {
    return {
      moment,
      visibleImport:false,
      disableMixinCreated:true,
      form: null,
      mdl: {},
      url:{
        list:'/volunteerTeam/list',
        add:'/volunteerTeam/add',
        edit:'/volunteerTeam/edit',
        delete:'/volunteerTeam/delete',
        deleteBatch:'/volunteerTeam/deleteBatch',
        exportXlsUrl:'/volunteerTeam/exportXls'
      },
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        actStatus:10
      },
      // 表头
      columns: [
        {
          title: '队伍名称',
          align: 'center',
          width:220,
          dataIndex: 'name'
        },
        {
          title: '所属部门',
          align: 'center',
          width:150,
          dataIndex: 'departName'
        },
        // {
        //   title: '院系',
        //   align: 'center',
        //   dataIndex: 'college'
        // },
        {
          title: '负责人',
          align: 'center',
          width:150,
          dataIndex: 'director'
        },
        {
          title: '指导老师',
          align: 'center',
          width:150,
          dataIndex: 'instructor'
        },
        {
          title: '成立时间',
          align: 'center',
          width:250,
          dataIndex: 'buildTime',
        },
        {
          title: '解散时间',
          align: 'center',
          width:250,
          dataIndex: 'dismissTime',
        },
        {
          title: '操作',
          align: 'center',
          // dataIndex: 'action',
          fixed:"right",
          width: 250,
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      getData: parameter => {
        let params = Object.assign({},parameter, this.queryParam)
        params.name&&(params.name = `*${params.name}*`)
        params.director&&(params.director = `*${params.director}*`)
        return getVolunteerTeams(params).then(res => {
          if(res.success&&res.result){
            const obj = {
              data: res.result.records||[],
              pageNo: res.result.current,
              pageSize: res.result.size,
              totalCount: res.result.total,
              totalPage: res.result.page,
              sortColumns: res.result.sortColumns
            }
            return obj
          }else{
            return {}
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    // 添加
    handleAdd(){
      this.$refs.VolunteerTeamModal.title = '添加志愿者队伍'
      this.$refs.VolunteerTeamModal.add();
    },
    // 申请
    handleCreate(){
      this.$refs.VolunteerTeamModal.title = '申请创建志愿者队伍'
      this.$refs.VolunteerTeamModal.apply({
        directorId: this.$store.getters.userInfo.id,
        director: this.$store.getters.userInfo.username,
      });
    },
    // 编辑
    handleEdit(record){
      this.$refs.VolunteerTeamModal.title = '编辑'
      this.$refs.VolunteerTeamModal.edit(record);
    },
    // 查看
    handleReview(record){
      this.$refs.VolunteerTeamModal.review(record);
    },
    // 申请加入
    handleJoin(record){
      this.$refs.VolunteerTeamMemberModal.title = '申请加入队伍'
      this.$refs.VolunteerTeamMemberModal.apply({
        teamId:record.id,
        teamName:record.name,
        userId: this.$store.getters.userInfo.id,
        userName: this.$store.getters.userInfo.username,
      },{ userId: true,teamName:true });
    },
    searchReset() {
      this.queryParam = {
        actStatus:10
      }
      this.searchQuery();
    },
    searchQuery() {
      this.$nextTick(() => {
        this.$refs.table.refresh()
      })
    },
    /**
     * 处理
     */
    handleDeal(record) {
      this.mdl = Object.assign({}, record)
      console.log(this.mdl)
      this.visible = true
    },
    handleDelete(id){
      let that = this
      deleteAction(this.url.delete,{id}).then(res =>{
          if(res.success){
            that.$message.success('删除成功');
            that.searchQuery()
          }else{
            that.$message.error(res.message)
          }
      })
    },
    batchDel: function () {
      if(!this.url.deleteBatch){
        this.$message.error("请设置api.leteBatch属性!")
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return;
      } else {
        var ids = "";
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ",";
        }
        var that = this;
        this.$confirm({
          title: "确认删除",
          content: "是否删除选中数据?",
          onOk: function () {
            that.loading = true;
            deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.selectedRowKeys = [];
                that.selectionRows = [];
                that.searchQuery()
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.loading = false;
            });
          }
        });
      }
    },

    onChange(row) {
      this.selectedRowKeys = row.selectedRowKeys
      this.selectedRows = row.selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },

    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
